const express = require('express');
const router = express.Router();
const staffInviteMapper = require('../mapper/StaffInviteMapper');
const conn = require('../utils/conn');
const jwt = require('../utils/jwt');

router.post('/v1/invite', (req, res) => {
  const validate = jwt.validate(req, res);
  if(validate < 1) {
    return;
  }

  const { userId, companyId } = req.body;
  if(userId != validate) {
    return res.send({ code: 418, err: "I'm a teapot" });
  }

  conn.query(staffInviteMapper.add, [ userId, companyId ], (err, data) => {
    if(err) {
      res.send({ code: 500, err });
    } if(data) {
      res.send({ code: 200, data });
    }
  });
});

router.get('/v1/invite', (req, res) => {
  const validate = jwt.validate(req, res);
  if(validate < 1) {
    return;
  }

  const { userId, companyId } = req.query;
  conn.query(staffInviteMapper.select, [ userId, userId, companyId, companyId ], (err, data) => {
    if(err) {
      res.send({ code: 500, err });
    } if(data) {
      res.send({ code: 200, data });
    }
  });
});

module.exports = router;